package com.mystudy.springcloud.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.mystudy.springcloud.entities.CommonResult;
import com.mystudy.springcloud.entities.Payment;
import com.mystudy.springcloud.myhandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {
	/**
	 * @功能: 测试按资源名称限流
	 */
	@GetMapping("/byRateLimit")
	@SentinelResource(value = "byRateLimit", blockHandler = "handleException")
	public CommonResult<Payment> byRateLimit() {
		return new CommonResult<Payment>(200, "测试按资源名称限流", new Payment(1L, "黑猫警长"));
	}

	/**
	 * @功能: 测试按URL限流
	 */
	@GetMapping("/byUrlLimit")
	@SentinelResource(value = "byUrlLimit", blockHandler = "handleException")
	public CommonResult<Payment> byUrlLimit() {
		return new CommonResult<Payment>(200, "测试按URL限流", new Payment(2L, "加菲猫警长"));
	}

	/**
	 * @功能: 兜底方法
	 */
	public CommonResult<Payment> handleException(BlockException exception) {
		return new CommonResult<Payment>(444, exception.getClass().getCanonicalName() + "\t 服务繁忙，请稍后再试！");
	}

	// -----------------------------------------------

	/**
	 * @功能: 测试使用客户自定义类里面的兜底方法
	 */
	@GetMapping("/customizeBlockHandler")
	@SentinelResource(value = "customizeBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
	public CommonResult<Payment> customizeBlockHandler() {
		return new CommonResult<Payment>(200, "测试使用客户自定义类里面的兜底方法", new Payment(3L, "汤姆猫警长"));
	}
}
